﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout
{
    public class DynamicDictionary<TValue> : DynamicObject, IDictionary<string, TValue>
    {
        private readonly Func<IDictionary<string, TValue>> dictionaryThunk;

        public DynamicDictionary(Func<IDictionary<string, TValue>> dictionaryThunk)
        {
            this.dictionaryThunk = dictionaryThunk;
        }
        public DynamicDictionary(IDictionary<string, TValue> dictionary)
            : this(() => dictionary)
        {
        }
        protected IDictionary<string, TValue> Dictionary
        {
            get { return dictionaryThunk(); }
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = Dictionary[binder.Name];
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            Dictionary[binder.Name] = (TValue)value;
            // you can always set a key in the dictionary so return true
            return true;
        }

        public void Clear()
        {
            Dictionary.Clear();
        }


        public bool IsReadOnly
        {
            get { return Dictionary.IsReadOnly; }
        }

        public int Count
        {
            get { return Dictionary.Count; }
        }

        public void Add(string key, TValue value)
        {
            Dictionary.Add(key, value);
        }

        public bool ContainsKey(string key)
        {
            return Dictionary.ContainsKey(key);
        }

        ICollection<string> IDictionary<string, TValue>.Keys
        {
            get { return Dictionary.Keys; }
        }

        public bool Remove(string key)
        {
            return Dictionary.Remove(key);
        }

        public bool TryGetValue(string key, out TValue value)
        {
            return this.Dictionary.TryGetValue(key, out value);
        }

        ICollection<TValue> IDictionary<string, TValue>.Values
        {
            get { return this.Dictionary.Values; }
        }

        public TValue this[string key]
        {
            get
            {
                return this.Dictionary[key];
            }
            set
            {
                this.Dictionary[key] = value;
            }
        }

        public void Add(KeyValuePair<string, TValue> item)
        {
            this.Dictionary.Add(item);
        }

        public bool Contains(KeyValuePair<string, TValue> item)
        {
            return this.Dictionary.Contains(item);
        }

        public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
        {
            Dictionary.CopyTo(array, arrayIndex);
        }

        public bool Remove(KeyValuePair<string, TValue> item)
        {
            return Dictionary.Remove(item);
        }

        IEnumerator<KeyValuePair<string, TValue>> IEnumerable<KeyValuePair<string, TValue>>.GetEnumerator()
        {
            return Dictionary.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.Dictionary.GetEnumerator();
        }
    }
}
